package com.yc.exam.web;

import com.yc.exam.bean.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptorUser implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User myinfo = (User) session.getAttribute("myinfo");
        boolean bool = (myinfo != null) && ("用户".equals(myinfo.getRole()));
        if(bool == false){
            if(!"用户".equals(myinfo.getRole())){
                System.out.println("当前用户状态异常，请管理员检查该用户状态");
            }
            response.sendRedirect("login.html?1");
        }
        session.setAttribute("requestURI", request.getRequestURI());
        return bool;
    }
}
